<template>
    <ToolBar :left-items="leftItems"/>
</template>

<script>
import tnx from '@/tnx.js';
import ToolBar from '../../../tnxjs/tnxvue3/src/element-plus/toolbar/Toolbar.vue';
import open from '@/renderer/pages/maven/open.js';
import About from '@/renderer/pages/help/about.vue';

export default {
    components: {ToolBar},
    data() {
        let inModeling = this.isInModeling();
        return {
            leftItems: [{
                icon: 'FolderOpened',
                caption: '打开...',
                click: () => {
                    open(this);
                },
            }, '|', {
                icon: 'fa-regular fa-floppy-disk',
                caption: '全部保存',
                disabled: !inModeling,
                click: () => {
                    tnx.app.eventBus.emit('tnxtool.modeling.saveAll');
                },
            }, {
                icon: 'bi bi-code-slash',
                caption: '批量生成代码',
                disabled: !inModeling,
                active: false,
                click: () => {
                    let item = this.leftItems[3];
                    this.toBatchGenerateCode(!item.active);
                },
            }, '|', {
                icon: 'bi bi-info-circle',
                caption: '关于',
                click: () => {
                    tnx.open(About);
                },
            }],
        };
    },
    mounted() {
        this.$router.afterEach(() => {
            let inModeling = this.isInModeling();
            this.leftItems[2].disabled = !inModeling;
            this.leftItems[3].disabled = !inModeling;
        });
        tnx.app.eventBus.on('tnxtool.modeling.batchGenerateCode', active => {
            this.leftItems[3].active = active;
        });
    },
    unmounted() {
        tnx.app.eventBus.off('tnxtool.modeling.batchGenerateCode');
    },
    methods: {
        isInModeling() {
            let path = this.$route?.path;
            return path && path.startsWith('/modeling');
        },
        toBatchGenerateCode(active = true) {
            let item = this.leftItems[3];
            item.active = active;
            tnx.app.eventBus.emit('tnxtool.modeling.batchGenerateCode', item.active);
        },
    }
}
</script>

<style lang="scss" scoped>

</style>
